
package dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import DBM.DBManager;

public class SupportDAO {
	private final String	addSupportMail	= "INSERT INTO Mail( accountID, subject, email, content, dateTime, isRead) VALUES( ?, ?, ?, ?, ?, ? )";
	private DBManager		dbmanager		= null;

	public SupportDAO( DBManager DBM ) {
		this.dbmanager = DBM;
	}

	public void addSupportMail( int accountID, String subject, String email,
			String content, String dateTime, boolean isRead ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( addSupportMail );
			ppstm.setInt( 1, accountID );
			ppstm.setString( 2, subject );
			ppstm.setString( 3, email );
			ppstm.setString( 4, content );
			ppstm.setString( 5, getCurrentTime() );
			ppstm.setBoolean( 6, false );
			ppstm.execute();
		} catch( SQLIntegrityConstraintViolationException e ) {
			e.printStackTrace();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	// TODO get support mail list, remove support mail
	private String getCurrentTime() {
		Calendar cal = Calendar.getInstance();
		return getStringFromCalendar( cal.getTime() );
	}

	public String getStringFromCalendar( java.util.Date date ) {
		if( date == null )
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		return sdf.format( date.getTime() );
	}
}
